import pymysql
from pymysql import connections

from Operator import settings


def init(mysql: connections.Connection):
    """
    初始化数据库连接以及数据库相关信息
    :param mysql: connections.Connection
    :return: Container
    """

    # 实例化数据容器
    cursor = mysql.cursor()

    # 检测运营者是否注册
    select_operator_sql = 'select * from `operator` where email="' + settings.EMAIL + '"'
    cursor.execute(select_operator_sql)
    operator = cursor.fetchone()
    if operator is None:
        print('注册用户：' + settings.NAME)
        cursor.execute(
            'insert into `operator` (`name`,`nickname`,`email`) '
            'VALUES("' + settings.NAME + '","' + settings.NICKNAME + '","' + settings.EMAIL + '")')

        cursor.execute(select_operator_sql)
        operator = cursor.fetchone()

    mysql.commit()
    cursor.close()
    return operator
